package com.document.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.document.common.utils.AuthUserToken;
import com.document.common.utils.BeanUtils;
import com.document.common.utils.StringUtil;
import com.document.common.utils.UUIDUtils;
import com.document.dao.FileManageMapper;
import com.document.dao.FileNotesMapper;
import com.document.entity.po.FileManageEntity;
import com.document.entity.po.FileNotesEntity;
import com.document.entity.request.FileNotesRequest;
import com.document.entity.response.FileNotesResponse;
import com.document.service.iservice.FileNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FileNoteServicelmpl  implements FileNoteService {
	@Autowired
	private FileNotesMapper FileNotesMapper;
	@Autowired
	private FileManageMapper fileManageMapper;

	@Override
	public IPage<FileNotesResponse> queryAll(FileNotesRequest request) {
		String user = AuthUserToken.getUserName();
		Page<FileNotesEntity> page = new Page<>(request.getPageNum(),request.getPageSize());
		page.setSearchCount(true);
		QueryWrapper<FileNotesEntity> wrapper = new QueryWrapper<FileNotesEntity>()
				.eq(!AuthUserToken.isAdmin(),"create_user_name",user)
				.like(StringUtil.isNotEmpty(request.getContent()),"content",request.getContent())
				.eq(StringUtil.isNotEmpty(request.getFileId()),"file_id",request.getFileId())
				.orderByDesc("create_time");
		IPage<FileNotesEntity> pageResponse = FileNotesMapper.selectPage(page,wrapper);
		List<FileNotesResponse>  list = new ArrayList<>();
		if(null != pageResponse.getRecords() && pageResponse.getRecords().size() >0){
			for (FileNotesEntity fileNotesEntity : pageResponse.getRecords()){
				FileManageEntity fileManageEntity = fileManageMapper.selectById(fileNotesEntity.getFileId());
				FileNotesResponse copy = BeanUtils.copy(fileNotesEntity, FileNotesResponse.class);
				copy.setFileName(null == fileManageEntity?"":fileManageEntity.getFileName());
				list.add(copy);
			}
		}
		Page<FileNotesResponse> responsePage = new Page<>();
		responsePage.setTotal(pageResponse.getTotal());
		responsePage.setRecords(list);
		return responsePage;
	}
	@Override
	public int save(FileNotesRequest request) {
		FileNotesEntity bean = BeanUtils.copy(request, FileNotesEntity.class);
		bean.setId(UUIDUtils.UUIDReplace());
		bean.setCreateTime(new Date());
		bean.setCreateUserName(AuthUserToken.getUserName());
		// 如果用户输入密码。进行MD5加密

		return FileNotesMapper.insert(bean);
	}


	@Override
	public int update(FileNotesRequest request) {
		FileNotesEntity bean = BeanUtils.copy(request, FileNotesEntity.class);
		return FileNotesMapper.updateById(bean);
	}
	@Override
	public void deleteById(String[] ids) {
		for(String id : ids){
			FileNotesMapper.deleteById(id);
		}
	}


}
